#!/usr/bin/env zsh
export NVM_DIR="$HOME/.nvm"
source "$NVM_DIR/nvm.sh"
nvm use v22.14.0

# start all mempool backends that exist
for site in mainnet mainnet-lightning testnet testnet-lightning testnet4 signet signet-lightning liquid liquidtestnet;do
    [ ! -e "${HOME}/${site}/backend/" ] && continue
    cd "${HOME}/${site}/backend/" && \
    echo "starting mempool backend: ${site}" && \
    screen -dmS "${site}" sh -c 'while true;do npm run start-production;sleep 1;done'
done

export DISPLAY=:0
screen -dmS x startx
sleep 3

# start unfurlers for each frontend
for site in mainnet liquid onbtc bitb meta river strategy xxi;do
    cd "$HOME/${site}/unfurler" && \
    echo "starting mempool unfurler: ${site}" && \
    screen -dmS "unfurler-${site}" sh -c 'while true;do npm run unfurler;sleep 2;done'
done

# start nginx warm cacher
for site in mainnet;do
    echo "starting mempool cache warmer: ${site}"
    screen -dmS "warmer-${site}" $HOME/mempool/production/nginx-cache-warmer
done

# start nginx hot cacher
for site in mainnet;do
    echo "starting mempool cache heater: ${site}"
    screen -dmS "heater-${site}" $HOME/mempool/production/nginx-cache-heater
done

exit 0
